<template>
  <div class="container">
    <div class="main">
      <div class="left">
        <img src="../assets/show.jpg" alt="展示图" />
      </div>
      <div class="right">
        <div class="login">
          <div class="title">用户登录</div>
          <div class="form">
            <div class="item">
              <el-input
                prefix-icon="el-icon-user"
                size="large"
                placeholder="请输入用户名"
                v-model="user.mobile"
                onkeyup="this.value=this.value.replace(/^\s+|\s+$/g,'')"
              ></el-input>
            </div>
            <div class="item">
              <el-input
                prefix-icon="el-icon-lock"
                size="large"
                placeholder="请输入密码"
                v-model="user.password"
                show-password
                onkeyup="this.value=this.value.replace(/^\s+|\s+$/g,'')"
              ></el-input>
            </div>
            <div style="height:14px">
              <p class="errtext">{{errtext}}</p>
            </div>
            <div class="item">
              <el-button type="primary" class="loginBtn" @click="goManage">登录</el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      user: { mobile: "", password: "" },
      errtext: "",
    };
  },
  created() {
    //开启监听键盘按下事件
    window.addEventListener("keydown", this.handleKeyDown, true);
  },
  methods: {
    //回车键登录
    handleKeyDown() {
      let key = null;
      if (window.event === undefined) {
        key = e.keyCode;
      } else {
        key = window.event.keyCode;
      }
      if (key === 13) {
        this.goManage();
      }
    },
    goManage() {
      if (this.user.mobile != "" && this.user.password != "") {
        this.errtext = "登录中...";
        this.$axios
          .post("/api/login", this.user)
          .then(res => {
            if (res.data.code == "0000" && res.status == 200) {
              this.$store.commit("login", res.data.data);
              let newTime = new Date().getTime();
              localStorage.setItem("oldTime", JSON.stringify(newTime));
              this.errtext = "";
              this.user.mobile = "";
              this.user.password = "";
              this.$router.push({ path: "/manage/home" });
            } else {
              this.errtext = "用户名或密码错误!";
            }
          })
          .catch(err=>{
            this.errtext = "网络异常,请稍后再试!"
          });
      } else {
        this.errtext = "账号和密码不能为空!";
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.container {
  width: 1200px;
  margin: 0 auto;
  .main {
    width: 100%;
    display: flex;
    .left {
      flex: 3;
      text-align: right;
      margin-right: 30px;
      img {
        height: 406px;
        border-radius: 8px;
        box-shadow: 0 0 8px black;
      }
    }
    .right {
      flex: 2;
      .login {
        width: 320px;
        height: 400px;
        border: 1px solid #0075a9;
        .title {
          border-bottom: 1px solid #0075a9;
          height: 50px;
          font-size: 24px;
          text-align: center;
          line-height: 50px;
          color: #0b4f6d;
        }
        .form {
          margin-top: 20px;
          padding: 20px;
          text-align: center;
          .errtext {
            text-align: left;
            padding-left: 6px;
            height: 100%;
            color: #f56c6c;
            font-size: 12px;
          }
          .item {
            margin-top: 20px;
            margin-bottom: 10px;
            /deep/ .el-input__inner {
              height: 50px;
              line-height: 50px;
              border: 1px solid #0075a9;
              font-size: 16px;
              padding-left: 34px;
            }
            /deep/ .el-input__prefix {
              color: #0075a9;
              font-size: 20px;
              top: 2px;
            }
            .loginBtn {
              width: 100%;
              height: 50px;
              background-color: #0075a9;
              font-size: 20px;
              font-family: "宋体";
            }
          }
        }
      }
    }
  }
}
</style>